﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Spinning.Spinning.Unitity;
using Microsoft.Xna.Framework.Input.Touch;
using Spinning.Spinning.Scenes.ProcessScene;

namespace Spinning.Spinning.Entity.Scenes
{
    class RoleScene : BaseScene
    {
        TextEntity scoreText;
        public Constants.PlayerId CharID
        {
            get;
            set;
        }
        public Constants.SpinningType SpinId
        {
            get;
            set;
        }

        AnimationSprite _char;
        AnimationSprite _names;
        AnimationSprite _spinings;

        List<AnimationSprite> _items = new List<AnimationSprite>();

        public override void OnLoad()
        {
            base.OnLoad();
            Sprite backGroundSprite = new Sprite();
            backGroundSprite.textureName = "role/interface01_bg";
            backGroundSprite.ZIndex = Constants.background;
            backGroundSprite.OnLoad();
            this.Attach(backGroundSprite);

            makeButtons();

            scoreText = new TextEntity();
            ResourceManager.ContentRoot = "";
            scoreText.fontString = "Century Gothic";
            scoreText.OnLoad();
            scoreText.Scale = new Vector2(1.2f);
            scoreText.Origin = new Vector2(1);
            scoreText.Position = new Vector2(430, 508);
            scoreText.Color = Color.Black;
            //te.Alpha = 0;
            scoreText.ZIndex = Constants.ui;
            Attach(scoreText);
            scoreText.Value = Game1.stageManager.GameSrate.TotalScore.ToString();

            _char = new AnimationSprite();
            _char.textureName = "role/char";
            _char.Position = new Vector2(349, 189);
            makeChar(_char);

            _names = new AnimationSprite();
            _names.textureName = "role/names";
            _names.Position = new Vector2(141.5f, 302.5f);
            makeChar(_names);

            _spinings = new AnimationSprite();
            _spinings.textureName = "role/spinnings";
            _spinings.Position = new Vector2(141.5f, 148.5f);
            makeChar(_spinings);
            _spinings.SetAnimation(new Point(2, 3), new int[] { 0 });

            //List<Unitity.Constants.ItemTypes> list = new List<Constants.ItemTypes>();
            //list.Add(Unitity.Constants.ItemTypes.Doll);
            //list.Add(Unitity.Constants.ItemTypes.Sandbag);
            //list.Add(Unitity.Constants.ItemTypes.Copter);
            //list.Add(Unitity.Constants.ItemTypes.Marble);
            makeItems(Game1.stageManager.GameSrate.ChooseItem);

            
        }

        private void makeItems(List<Spinning.Unitity.Constants.ItemTypes> itemIds)
        {
            //int i = 0;
            List<Unitity.Constants.ItemTypes> removelist = new List<Constants.ItemTypes>();
            for (int i = 0; i < itemIds.Count; i++)
            {
                int number = 0;
                switch (itemIds[i])
                {
                    case Constants.ItemTypes.Marble:  //0
                        number = 0;
                        break;
                    case Constants.ItemTypes.Sandbag: //1
                        number = 1;
                        break;
                    case Constants.ItemTypes.Copter:  //2
                        number = 2;
                        break;
                    case Constants.ItemTypes.Windmill://3
                        number = 3;
                        break;
                    case Constants.ItemTypes.Doll:    //4
                        number = 4;
                        break;
                }
                if (Game1.stageManager.GameSrate.BuyedItems[number] == 0) removelist.Add(itemIds[i]);
            }

            foreach (Spinning.Unitity.Constants.ItemTypes type in removelist)
            {
                Game1.stageManager.GameSrate.ChooseItem.Remove(type);
            }
            itemIds = Game1.stageManager.GameSrate.ChooseItem;

            Vector2 basepos = new Vector2(84, 397.5f);//102
            foreach (Spinning.Unitity.Constants.ItemTypes type in itemIds)
            {
                Item item = new Item();
                item.OnLoad();
                item.ItemType = type;
                item.Position = basepos;
                basepos.X += 102;
                item.ZIndex = Constants.ui;
                Attach(item);
            }


        }

        private void makeButtons()
        {
            ButtonSprite storeSprite = new ButtonSprite();
            storeSprite.textureName = "role/btn_store";
            storeSprite.Position = new Vector2(35, 530);
            makeButton(storeSprite);

            ButtonSprite itemSprite = new ButtonSprite();
            itemSprite.textureName = "role/btn_items";
            itemSprite.Position = new Vector2(245, 530);
            makeButton(itemSprite);

            ButtonSprite single_pSprite = new ButtonSprite();
            single_pSprite.textureName = "role/btn_single_p";
            single_pSprite.Position = new Vector2(35, 530 + 120);
            makeButton(single_pSprite);

            ButtonSprite two_pSprite = new ButtonSprite();
            two_pSprite.textureName = "role/btn_two_p";
            two_pSprite.Position = new Vector2(245, 530 + 120);
            makeButton(two_pSprite);

            TouchManager.RegisteTouchArea(storeSprite);
            TouchManager.RegisteTouchArea(itemSprite);
            TouchManager.RegisteTouchArea(single_pSprite);
            TouchManager.RegisteTouchArea(two_pSprite);
            storeSprite.OnTouchEvent += OnStoreTouch;
            itemSprite.OnTouchEvent += OnItemTouch;
            single_pSprite.OnTouchEvent += OnSingleTouch;
            two_pSprite.OnTouchEvent += OnTwo_pTouch;
        }

        private void makeButton(ButtonSprite sprite)
        {
            sprite.ZIndex = Constants.ui;
            sprite.OnLoad();
            this.Attach(sprite);
        }

        private void makeChar(AnimationSprite _char)
        {
            _char.OnLoad();
            _char.SetAnimation(new Point(1, 3), new int[] { 0 });
            _char.ZIndex = Constants.ui;
            _char.Origin = new Vector2(.5f);
            Attach(_char);
        }

        protected override void UpdateSelf(Microsoft.Xna.Framework.GameTime gameTime)
        {
            base.UpdateSelf(gameTime);
            NewMethodChar(CharID);
            NewMethodSpinning(SpinId);


        }

        private void NewMethodChar(Constants.PlayerId _player)
        {
            _char.SetChangeFrameAnimation(new int[] { Constants.MappingPlayer(_player) });
            _names.SetChangeFrameAnimation(new int[] { Constants.MappingPlayer(_player) });
        }

        private void NewMethodSpinning(Constants.SpinningType spin)
        {
            _spinings.SetChangeFrameAnimation(new int[] { Constants.MappingSpinning(spin) });
        }

        void OnStoreTouch(BaseEntity entity, TouchLocation touchLocation)
        {
            Game1.stageManager.GoToScene("store");
        }

        void OnItemTouch(BaseEntity entity, TouchLocation touchLocation)
        {
            Game1.stageManager.GoToScene("item");
        }

        void OnSingleTouch(BaseEntity entity, TouchLocation touchLocation)
        {
            Game1.stageManager.GoToScene("singleMenu");
        }

        void OnTwo_pTouch(BaseEntity entity, TouchLocation touchLocation)
        {
            Game1.stageManager.GoToScene("connect");
        }

        public override BaseScene Clone()
        {
            RoleScene scene = new RoleScene();
            scene.Size = new Vector2(Constants.Window_Width, Constants.Window_Height);
            scene.OnLoad();
            scene.SortChildren();

            scene.CharID = Game1.stageManager.GameSrate.CurrentPlayer;
            scene.SpinId = Game1.stageManager.GameSrate.PlayerSpinningType;

            return scene;
        }
    }
}
